package com.tgu.home_action.service.impl;

import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.DynamicMapper;
import com.tgu.home_action.dao.FaceMapper;
import com.tgu.home_action.dao.VolunteerInformationMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.LargeScreenService;
import com.tgu.home_action.service.utils.DateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class LargeScreenServiceImpl implements LargeScreenService {
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private VolunteerInformationMapper volunteerInformationMapper;
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private FaceMapper faceMapper;

    /**
     * 获取数据
     */
    @Override
    public ResponseData<LargeScreenData> getData(Place place) throws ParseException {
        LargeScreenData largeScreenData=new LargeScreenData();
        largeScreenData.setCaseMap(getIngCaseToday(place));
        largeScreenData.setCaseNum(getCaseNum(place));
        largeScreenData.setCaseTop(getTopCasePlace());
        largeScreenData.setDevelopmentTrend(getHourCaseNum(place));
//        largeScreenData.setFaceList(getFace(place));
        largeScreenData.setDynamicList(getDynamic(place));
        largeScreenData.setOnlineVolunteerNum(getOnlineVolunteerNumber(place));
        largeScreenData.setIngCaseNum(getIngCaseNum(place));
        largeScreenData.setTodayFinishCaseNum(getTodayFinishCaseNum(place));
        largeScreenData.setTodayNewCaseNum(getTodayNewCaseNum(place));
        largeScreenData.setFinishCaseNum(getFinishCaseNum(place));
        largeScreenData.setRegisterVolunteerNum(getRegisterNum(place));
        largeScreenData.setOverTimeCaseNum(getOverTimeCaseNum(place));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),largeScreenData);
    }

    /**
     * 查询今日新增案件数
     */
    public int getTodayNewCaseNum(Place place) throws ParseException {
        place.setTime(DateService.getTodayTimestamp());
        return caseMapper.getTodayNewCaseNum(place);
    }

    /**
     * 查询今日完成案件数
     */
    public int getTodayFinishCaseNum(Place place) throws ParseException {
        place.setTime(DateService.getTodayTimestamp());
        return caseMapper.getTodayFinishCaseNum(place);
    }

    /**
     * 获取在线的志愿者数量
     */
    public int getOnlineVolunteerNumber(Place place) {
        Set<String> volunteers = stringRedisTemplate.opsForSet().members(RedisHeader.VOLUNTEER_ONLINE.getHeader());
        int number = 0;
        List<Integer> volunteerIds = new ArrayList<>();
        if (volunteers != null) {
            for (String volunteerId : volunteers) {
                volunteerIds.add(Integer.valueOf(volunteerId));
            }
            number = volunteerInformationMapper.getOnlineVolunteerNumber(place.getVolunteerInformation(), volunteerIds);

        }
        return number;
    }

    /**
     * 进行中案件总数
     */
    public int getIngCaseNum(Place place){
        Case nowCase=place.getCase();
        nowCase.setState((short) 1);
        return caseMapper.getAccordCaseNumber(nowCase);
    }

    /**
     * 已完成案件总数
     */
    public int getFinishCaseNum(Place place){
        Case nowCase=place.getCase();
        nowCase.setState((short) 2);
        return caseMapper.getAccordCaseNumber(nowCase);
    }

    /**
     * 存量案件总数
     */
    public int getCaseNum(Place place){
        Case nowCase=place.getCase();
        return caseMapper.getAccordCaseNumber(nowCase);
    }

    /**
     * 查询已归档案件数
     */
    private int getOverTimeCaseNum(Place place){
        Case nowCase=place.getCase();
        nowCase.setState((short) 3);
        return caseMapper.getAccordCaseNumber(nowCase);
    }

    /**
     * 查询案件动态
     */
    public List<Dynamic> getDynamic(Place place){
        place.setTime(DateService.getBeforeDay());
        return dynamicMapper.getDynamicByPlace(place);
    }

    /**
     * 查询人脸识别动态
     */
    public List<Face> getFace(Place place){
        place.setTime(DateService.getBeforeDay());
        return faceMapper.getFaceByPlace(place);
    }

    /**
     * 查询案发Top5
     */
    public List<Place> getTopCasePlace() throws ParseException {
        //        System.out.println(places);
        return caseMapper.getTopCasePlace(DateService.getBeforeDay());
    }

    /**
     * 查询每小时的案件数
     */
    public List<Place> getHourCaseNum(Place place){
        place.setTime(DateService.getBeforeDay());
        //        System.out.println(places);
        return caseMapper.getHourCaseNum(place);
    }

    /**
     * 24小时内正在发生的案件
     */
    public List<Case> getIngCaseToday(Place place){
        place.setTime(DateService.getBeforeDay());
        return caseMapper.getIngCaseToday(place);
    }

    /**
     * 获取已注册的志愿者数
     */
    public int getRegisterNum(Place place){
        Admin admin=place.getAdmin();
        return volunteerInformationMapper.getRegisterNum(admin);
    }



}
